package com.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.ErrorCardDao;
import com.modal.ErrorCardModel;
import com.pojo.ErrorCard;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ErrorCardService extends ServiceImpl<ErrorCardDao, ErrorCard> {

    @Autowired
    private ErrorCardDao errorCardDao;

    public String insertErrorCard(ErrorCardModel m){
        ErrorCard errorCard = new ErrorCard();
        BeanUtil.copyProperties(m, errorCard);
        if (errorCardDao.insert(errorCard) < 1) {
            return "新增错误卡失败";
        }
        return null;
    }

    public String updateErrorCard(ErrorCardModel m){
        ErrorCard errorCard = errorCardDao.selectById(m.getId());
        if (errorCard == null) {
            return "该错误卡不存在";
        }
        BeanUtil.copyProperties(m, errorCard);
        if (errorCardDao.updateById(errorCard) < 1) {
            return "编辑错误卡失败";
        }
        return null;
    }

    public String deleteErrorCard(ErrorCardModel m){
        if (StringUtil.isNull(m.getId())) {
            return "没有错误卡id";
        }
        ErrorCard errorCard = errorCardDao.selectById(m.getId());
        if (errorCard == null) {
            return "错误卡不存在";
        }
        if (errorCardDao.deleteById(m.getId()) < 1) {
            return "删除错误卡失败";
        }
        return null;
    }
}